package com.bdqn.service;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.util.MyBatisUtil;
import com.bdqn.util.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户服务层实现类
 *
 * @author LILIBO
 * @since 2021-11-18
 */
public class SysUserServiceImpl implements SysUserService {

    private Logger logger = Logger.getLogger(SysUserServiceImpl.class);

    /**
     * 用户登录
     *
     * @param account
     * @param password
     * @return
     */
    @Override
    public SysUser login(String account, String password) {
        if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
            return null;
        }
        SysUser sysUser = null;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        try {
            sysUser = sqlSession.getMapper(SysUserMapper.class).findByAccountAndPassword(account, password);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return sysUser;
    }

    /**
     * 根据查询条件获得用户数量
     *
     * @param querySysUser
     * @return
     */
    @Override
    public int getCount(SysUser querySysUser) {
        if (querySysUser == null) {
            return 0;
        }
        int count = 0;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        try {
            count = sqlSession.getMapper(SysUserMapper.class).getCount(querySysUser);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据查询条件获得分页用户数据
     *
     * @param querySysUser
     * @param page
     * @return
     */
    @Override
    public List<SysUser> getPageList(SysUser querySysUser, Page<SysUser> page) {
        if (querySysUser == null || page == null) {
            return null;
        }
        List<SysUser> list = new ArrayList<>();
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        try {
            list = sqlSession.getMapper(SysUserMapper.class).getPageList(querySysUser, ((page.getCurrPageNo() - 1) * page.getPageSize()), page.getPageSize());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 根据ID获取用户
     *
     * @param id
     * @return
     */
    @Override
    public SysUser getById(Long id) {
        SysUser sysUser = null;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        try {
            sysUser = sqlSession.getMapper(SysUserMapper.class).getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return sysUser;
    }

    /**
     * 根据ID删除用户
     *
     * @param id
     * @return
     */
    @Override
    public int delById(Long id) {
        int result = 0;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        try {
            result = sqlSession.getMapper(SysUserMapper.class).delById(id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
}
